<?php

class st_user_log_model extends stmodel_2_1 {

    public function __construct($config) {
        parent::__construct($config);
        $this->setTableName($this->prefix . 'st_user_log');
        $this->setPrimaryKey('id');
    }

    public function saveCronLog($data) {
        $data['id'] = isset($data['id']) ? $data['id'] : NULL;

        return $this->saveRecords($data);
    }

    public function getID($name, $ctime) {
        $query = "SELECT id FROM `{$this->_tableName}`
        WHERE `ctime` like('{$ctime}') AND `cronjob_name` like('{$name}')
        ";
//        echo $query; die;
        return $this->get_var($query);
    }

    public function countRecords($args = '') {
        $result = $this->getRecords($args);
        return count($result);
    }

    public function getCronjobName() {
        $query = "SELECT cronjob_name FROM `{$this->_tableName}`
        GROUP BY cronjob_name
        ";
//        echo $query; die;
        return $this->get_col($query);
    }

    public function cleanLog() {
        $month = intval(date('n')) - 3;
        $query = "SELECT * FROM $this->_tableName
        WHERE month(date) < {$month}";
//        echo $query;die;
        if ($this->query($query)) {
            $query = "DELETE FROM $this->_tableName
            WHERE month(date) < {$month}";
//            echo $query;die;
            return $this->query($query);
        } else {
            return true;
        }
    }

    public function numberOfLogin() {
        $query = "SELECT status, count(id) AS num FROM `{$this->_tableName}`
        GROUP BY status
        ORDER BY status ASC
        ";
//        echo $query; die;
        $result = $this->get_results($query);
        $results = array();
        $results['fail'] = $result[0]->num;
        $results['success'] = $result[1]->num;
        return $results;
    }

    public function getMostFiveLogin() {
        $query = "SELECT count(id) AS num, username FROM `{$this->_tableName}`        
        GROUP BY username
        ORDER BY `num` ASC
        LIMIT 0,5
        ";
//        echo $query;
        $results = $this->get_results($query);
        return $results;
    }

    public function numberOfRegisterUser() {
        $query = "SELECT count(id) AS num FROM `{$this->prefix}st_social_user`
        ";
//        echo $query; die;
        $result1 = $this->get_var($query);

        return $result1;
    }

    public function getDBSize() {
        $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

        $db_name = DB_NAME;
        $tables = array();

        mysql_select_db($db_name, $link);
        $result = mysql_query("SHOW TABLE STATUS");
        $total_size = 0;
        while ($row = mysql_fetch_array($result)) {
            /* We return the size in Kilobytes */
            $total_size += ($row["Data_length"] + $row["Index_length"]) / 1024;
        }
        return round($total_size/ 1024,2);
    }
    
    public function totalLogRow() {
        $query = "SELECT count(id) AS num FROM `{$this->prefix}st_cronjob_log`
        ";
        $result1 = $this->get_var($query);
        $query = "SELECT count(id) AS num FROM `{$this->_tableName}`
        ";
//        echo $query; die;
        $result2 = $this->get_var($query);

        return $result1 + $result2;
    }

}